package com.web;

import com.domain.User;
import com.service.userService;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
/**
 * 客户自己修改个人数据的数据作用
 * 接收 updateUserSelf.jsp 页面的 表单数据，封装成User对象
 * 提交到 userService 的 updateUser 函数
 * 重定向到 /ShowUserSelf
 */
@WebServlet("/updateUserSelf")
public class updateUserSelfServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决乱码
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        //1.接收表单传入的数据
        Map<String, String[]> map = request.getParameterMap();
        //2.封装成对象
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("客户:"+ user.getUserId() +"正在修改自己的数据,改为");
        System.out.println(user.toString());
        //3.调用service层,修改数据
        new userService().updateUser(user);
        //重定向到查询所有用户
        response.sendRedirect(request.getContextPath()+"/ShowUserSelf?userId="+user.getUserId());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
